package pers.lmd.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import pers.lmd.domain.R;
import pers.lmd.domain.dto.LoginDTO;
import pers.lmd.service.IUserService;
import pers.lmd.common.utils.BaiduAddressUtil;
import pers.lmd.common.utils.VerificationCodeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author lmd
 * @Date 2023/4/11 23:18
 */
@RequiredArgsConstructor
@RestController
@Tag(name = "用户控制器")
@RequestMapping("/system")
public class UserController {

    private final IUserService IUserService;
    private final HttpServletRequest request;
    private final HttpServletResponse response;

    @Operation(summary = "获取验证码")
    @GetMapping("/captcha/{uuid}")
    public void getCaptcha(@PathVariable String uuid) throws IOException {
        VerificationCodeUtil.createVerificationCode(request, response,uuid);
    }

    @Operation(summary = "登录")
    @PostMapping("/login/{uuid}")
    public R<SaTokenInfo> signIn(@RequestBody LoginDTO loginDTO,@PathVariable String uuid) {
        // 校验用户账号、密码、验证码
        return IUserService.login(loginDTO.getId(), loginDTO.getPassword(), loginDTO.getCaptcha(),uuid);
    }

    @Operation(summary = "获取用户当前城市")
    @GetMapping("/city")
    public R<String> getCity() {
        return BaiduAddressUtil.getAddress("");
    }

    @Operation(summary = "获取用户角色")
    @GetMapping("/role")
    public int getRole() {
        return IUserService.getRole();
    }


}
